﻿#include "frmain.h"
#include "ui_frmain.h"

#include <math.h>

FrMain::FrMain(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FrMain)
{
    ui->setupUi(this);
    initForm ();
    initButton ();
    initTitleText ("宽带传感器测试校准软件V1.1");
    initTabWidget ();

    initCustomplot1 ();
    initCustomplot2 ();
    initCustomplot3 ();
    initCustomplot4 ();

    initTab1 ();
    initTab2();
    initTab3();
    initTab4();
}

FrMain::~FrMain()
{
    delete ui;
}

void FrMain::initForm()
{
    m_totleHeight = 600;                //界面总高度
    m_Width = 800;                     //界面总宽度
    m_titleHeight = 40;                 //标题栏高度
    ui->tabWidget->setCurrentIndex (0);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute(Qt::WA_TranslucentBackground,true);
    this->resize(m_Width,m_totleHeight);

    this->setProperty("canMove", QVariant(true));

    QDesktopWidget *desktop = QApplication::desktop();
    this->move((desktop->width()-this->width())/2 , (desktop->height()-this->height())/2);
    this->setWindowOpacity (1);

   }

void FrMain::initButton()
{
    btnClose =new QPushButton(this);
    btnMin = new QPushButton(this);

    //QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QIcon close_buttonIcon(":/image/btn_close_normal.png");
    QIcon min_buttonIcon(":/image/btn_small_normal.png");

    btnClose->setIcon(close_buttonIcon);
    btnMin->setIcon(min_buttonIcon);
    btnClose->setGeometry(m_Width-40,9,30,30);
    btnMin->setGeometry (m_Width-70,9,30,30);
    btnClose->setStyleSheet("background-color:transparent;");
    btnMin->setStyleSheet("background-color:transparent;");
    //btnSetup->setStyleSheet("color:#7A95B0;background-color:transparent;");

    connect(btnClose,SIGNAL(clicked()),this,SLOT(btnMenu_Close_clicked()));
    connect(btnMin,SIGNAL (clicked()),this,SLOT(btnMenu_Min_clicked()));
}

void FrMain::initTitleText(QString str)
{
    lbTitleText=new QLabel(this);
    lbTitleText->move(54,m_titleHeight/3);
    QFont ft;
    ft.setPointSize(12);
    lbTitleText->setFont(ft);
    QPalette pa;
    pa.setColor(QPalette::WindowText,QColor("#ffcc33"));
    lbTitleText->setPalette(pa);
    lbTitleText->setText(str);
}

void FrMain::initTabWidget()
{
    ui->tabWidget->setTabText (0,"宽带发射传感器TVR测试");
    ui->tabWidget->setTabText (1,"发射指向性测试");
    ui->tabWidget->setTabText (2,"接收灵敏度测试");
    ui->tabWidget->setTabText (3,"接收指向性测试");
}

void FrMain::initCustomplot1()
{
    //让坐标系支持拖放，缩放，
    ui->plot1->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->plot1->setBackground(QColor(255, 255, 255));                  //设置背景色
    ui->plot1->addGraph()->setPen (QPen(QColor(50,50,50)));        //设置画笔颜色
    ui->plot1->xAxis->grid ()->setVisible (false);                 //去除垂直于x轴的默认网格线
    ui->plot1->yAxis->grid ()->setVisible (false);                 //去除垂直于y轴的默认网格线

    //设置x基准轴颜色
    ui->plot1->xAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot1->xAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot1->xAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot1->xAxis->setTickLabelColor(QColor(45, 140, 149));     //设置x轴刻度字体颜色

    ui->plot1->xAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot1->xAxis->setLabel("时间");

    //设置y基准轴颜色
    ui->plot1->yAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot1->yAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot1->yAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot1->yAxis->setTickLabelColor(QColor(45, 140, 149));     //设置y轴刻度字体颜色

    ui->plot1->yAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot1->yAxis->setLabel("幅度");
    QFont yFont = ui->plot1->yAxis->labelFont();               //获取y坐标轴“标签”字体

    //yFont.setPixelSize(20);                                         //设置y轴标签字体大小
     //设置yFont
    ui->plot1->yAxis->setLabelFont(yFont);


    // 设置坐标轴显示范围，否则只能看到默认范围
    ui->plot1->xAxis->setRange(0, 100, Qt::AlignLeft);
   // ui->customplot->xAxis->setRange(-100, 100);
    ui->plot1->yAxis->setRange(-4, 4);

    //ui->customplot->yAxis->setPadding (100);          //设置坐标轴距离边界的距离
    //ui->customplot->xAxis->setOffset (-50);

    ui->plot1->addGraph();
    ui->plot1->graph(0)->setPen(QPen(QColor(179,62,26)));
}

void FrMain::initCustomplot2()
{
    //让坐标系支持拖放，缩放，
    ui->plot2->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->plot2->setBackground(QColor(255, 255, 255));                  //设置背景色
    ui->plot2->addGraph()->setPen (QPen(QColor(50,50,50)));        //设置画笔颜色
    ui->plot2->xAxis->grid ()->setVisible (false);                 //去除垂直于x轴的默认网格线
    ui->plot2->yAxis->grid ()->setVisible (false);                 //去除垂直于y轴的默认网格线

    //设置x基准轴颜色
    ui->plot2->xAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot2->xAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot2->xAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot2->xAxis->setTickLabelColor(QColor(45, 140, 149));     //设置x轴刻度字体颜色

    ui->plot2->xAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot2->xAxis->setLabel("时间");

    //设置y基准轴颜色
    ui->plot2->yAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot2->yAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot2->yAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot2->yAxis->setTickLabelColor(QColor(45, 140, 149));     //设置y轴刻度字体颜色

    ui->plot2->yAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot2->yAxis->setLabel("幅度");
    QFont yFont = ui->plot2->yAxis->labelFont();               //获取y坐标轴“标签”字体

    //yFont.setPixelSize(20);                                         //设置y轴标签字体大小
     //设置yFont
    ui->plot2->yAxis->setLabelFont(yFont);


    // 设置坐标轴显示范围，否则只能看到默认范围
    ui->plot2->xAxis->setRange(0, 100, Qt::AlignLeft);
   // ui->customplot->xAxis->setRange(-100, 100);
    ui->plot2->yAxis->setRange(-4, 4);

    //ui->customplot->yAxis->setPadding (100);          //设置坐标轴距离边界的距离
    //ui->customplot->xAxis->setOffset (-50);

    ui->plot2->addGraph();
    ui->plot2->graph(0)->setPen(QPen(QColor(179,62,26)));
}

void FrMain::initCustomplot3()
{
    //让坐标系支持拖放，缩放，
    ui->plot3->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->plot3->setBackground(QColor(255, 255, 255));                  //设置背景色
    ui->plot3->addGraph()->setPen (QPen(QColor(50,50,50)));        //设置画笔颜色
    ui->plot3->xAxis->grid ()->setVisible (false);                 //去除垂直于x轴的默认网格线
    ui->plot3->yAxis->grid ()->setVisible (false);                 //去除垂直于y轴的默认网格线

    //设置x基准轴颜色
    ui->plot3->xAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot3->xAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot3->xAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot3->xAxis->setTickLabelColor(QColor(45, 140, 149));     //设置x轴刻度字体颜色

    ui->plot3->xAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot3->xAxis->setLabel("时间");

    //设置y基准轴颜色
    ui->plot3->yAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot3->yAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot3->yAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot3->yAxis->setTickLabelColor(QColor(45, 140, 149));     //设置y轴刻度字体颜色

    ui->plot3->yAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot3->yAxis->setLabel("幅度");
    QFont yFont = ui->plot3->yAxis->labelFont();               //获取y坐标轴“标签”字体

    //yFont.setPixelSize(20);                                         //设置y轴标签字体大小
     //设置yFont
    ui->plot3->yAxis->setLabelFont(yFont);


    // 设置坐标轴显示范围，否则只能看到默认范围
    ui->plot3->xAxis->setRange(0, 100, Qt::AlignLeft);
   // ui->customplot->xAxis->setRange(-100, 100);
    ui->plot3->yAxis->setRange(-4, 4);

    //ui->customplot->yAxis->setPadding (100);          //设置坐标轴距离边界的距离
    //ui->customplot->xAxis->setOffset (-50);

    ui->plot3->addGraph();
    ui->plot3->graph(0)->setPen(QPen(QColor(179,62,26)));
}

void FrMain::initCustomplot4()
{
    //让坐标系支持拖放，缩放，
    ui->plot4->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->plot4->setBackground(QColor(255, 255, 255));                  //设置背景色
    ui->plot4->addGraph()->setPen (QPen(QColor(50,50,50)));        //设置画笔颜色
    ui->plot4->xAxis->grid ()->setVisible (false);                 //去除垂直于x轴的默认网格线
    ui->plot4->yAxis->grid ()->setVisible (false);                 //去除垂直于y轴的默认网格线

    //设置x基准轴颜色
    ui->plot4->xAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot4->xAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot4->xAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot4->xAxis->setTickLabelColor(QColor(45, 140, 149));     //设置x轴刻度字体颜色

    ui->plot4->xAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot4->xAxis->setLabel("时间");

    //设置y基准轴颜色
    ui->plot4->yAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->plot4->yAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->plot4->yAxis->setSubTickPen(QColor(41, 113, 120));

    ui->plot4->yAxis->setTickLabelColor(QColor(45, 140, 149));     //设置y轴刻度字体颜色

    ui->plot4->yAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->plot4->yAxis->setLabel("幅度");
    QFont yFont = ui->plot4->yAxis->labelFont();               //获取y坐标轴“标签”字体

    //yFont.setPixelSize(20);                                         //设置y轴标签字体大小
     //设置yFont
    ui->plot4->yAxis->setLabelFont(yFont);


    // 设置坐标轴显示范围，否则只能看到默认范围
    ui->plot4->xAxis->setRange(0, 100, Qt::AlignLeft);
   // ui->customplot->xAxis->setRange(-100, 100);
    ui->plot4->yAxis->setRange(-4, 4);

    //ui->customplot->yAxis->setPadding (100);          //设置坐标轴距离边界的距离
    //ui->customplot->xAxis->setOffset (-50);

    ui->plot4->addGraph();
    ui->plot4->graph(0)->setPen(QPen(QColor(179,62,26)));
}

void FrMain::initTab1()
{
    ui->leDistanse->setText ("1");
    ui->leStartFreq->setText ("1");
    ui->leFreqSpace->setText("1");
    ui->leStopFreq->setText ("10");
    ui->leSignalWidth->setText ("0.01");
    ui->leSensitive->setText ("-201");
    ui->leVolExtract->setText ("8");
    ui->leMaxExtract->setText ("0.8");

    ui->btnStart->setStyleSheet ("border-style:none;border:1px solid #242424;color:#EEF8FC;padding:5px;border-radius:5px;background:#169BD5");

    connect(ui->btnStart,SIGNAL(clicked()),this,SLOT(btnStart_clicked()));


}

void FrMain::initTab2()
{
    ui->leDistanse2->setText ("1");
    ui->leSignalFreq2->setText ("1");
    ui->leStartAngle2->setText("0");
    ui->leAngleSpace2->setText ("10");
    ui->leSignalWidth2->setText ("0.01");
    ui->leSensitive2->setText ("-201");
    ui->leVolExtract2->setText ("8");
    ui->leMaxExtract2->setText ("0.8");

    ui->btnStart2->setStyleSheet ("border-style:none;border:1px solid #242424;color:#EEF8FC;padding:5px;border-radius:5px;background:#169BD5");

    connect(ui->btnStart2,SIGNAL(clicked()),this,SLOT(btnStart2_clicked()));

}

void FrMain::initTab3()
{
    ui->leDistanse3->setText ("1");
    ui->leStartFreq3->setText ("1");
    ui->leFreqSpace3->setText("1");
    ui->leStopFreq3->setText ("10");
    ui->leSignalWidth3->setText ("0.01");
    ui->leSensitive3->setText ("-201");
    ui->leVolExtract3->setText ("8");
    ui->leMaxExtract3->setText ("0.8");

    ui->btnStart3->setStyleSheet ("border-style:none;border:1px solid #242424;color:#EEF8FC;padding:5px;border-radius:5px;background:#169BD5");

    connect(ui->btnStart3,SIGNAL(clicked()),this,SLOT(btnStart3_clicked()));

}

void FrMain::initTab4()
{
    ui->leDistanse4->setText ("1");
    ui->leSignalFreq4->setText ("1");
    ui->leStartAngle4->setText("0");
    ui->leAngleSpace4->setText ("10");
    ui->leSignalWidth4->setText ("0.01");
    ui->leSensitive4->setText ("-201");
    ui->leVolExtract4->setText ("8");
    ui->leMaxExtract4->setText ("0.8");

    ui->btnStart4->setStyleSheet ("border-style:none;border:1px solid #242424;color:#EEF8FC;padding:5px;border-radius:5px;background:#169BD5");
    connect(ui->btnStart4,SIGNAL(clicked()),this,SLOT(btnStart4_clicked()));

}

void FrMain::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setBrush(QBrush(QColor("#169BD5")));
    painter.setPen(QPen(Qt::transparent));
    painter.drawRect(0,0,m_Width,m_titleHeight);

    painter.setPen(QPen("#000000"));
    painter.setBrush(QBrush(QColor("#FFFFFF")));
    painter.drawRect(0,m_titleHeight,m_Width-1,m_totleHeight-m_titleHeight-1);

    QPixmap pixmap;
    pixmap.load (":/image/FstLogo.ico");
    painter.drawPixmap (10,8,24,24,pixmap);
}

void FrMain::btnMenu_Close_clicked()
{
    this->close ();
}

void FrMain::btnMenu_Min_clicked()
{
    this->showMinimized ();
}

void FrMain::btnStart_clicked()
{
    qDebug()<<"按钮1点击了，开始计算";
    sv1.clear ();
    freq1.clear ();
    double distance = ui->leDistanse->text ().toDouble ();
    double ML = ui->leSensitive->text ().toDouble ();

    double startFreq = ui->leStartFreq->text ().toDouble ();
    double freqSpace = ui->leFreqSpace->text ().toDouble ();
    double stopFreq = ui->leStopFreq->text ().toDouble();

    int number =1 + (stopFreq-startFreq)/freqSpace;    //计算点数

    //double V = ui->leMaxExtract->text ().toDouble ();
    //double VLoad = ui->leVolExtract->text ().toDouble ();

    qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //设置随机数种子

    for(int i=0; i<number; i++)
    {
        //生成7.0到9.0之间的随机浮点数
        double VMax = 9.0;
        double VMin = 7.0;

        double VLoadMax = 0.9;
        double VLoadMin = 0.7;

        double diffV = fabs(VMax-VMin);
        double diffVLoad = fabs(VLoadMax-VLoadMin);

        double m1=(double)(qrand()%1000)/1000;//生成0.000到1.000的随机浮点数
        double retvalV=VMin+m1*diffV;
        double retvalVLoad=VLoadMin+m1*diffVLoad;

        //v1.push_back (retvalV);
        //vLoad1.push_back (retvalVLoad);
        //qDebug()<<retvalV;
        //qDebug()<<retvalVLoad;
        double tempSv1 = 20*log10(distance);
        double tempSv2 = 20*log10(retvalV/2.828);
        double tempSv3 = 20*log(retvalVLoad/2.828);
        double Sv = tempSv1-ML+tempSv2-tempSv3;

        sv1.push_back (Sv);         //纵坐标添加数据

        //qDebug()<<Sv;

    }

    for(int j=0 ; j<number ; j++)
    {
        freq1.push_back (startFreq+j*freqSpace);
    }


    SendSensorTVR* m_pSendSensorTVR = new SendSensorTVR(freq1,sv1);
    m_pSendSensorTVR->setModal (true);
    m_pSendSensorTVR->show ();
}

void FrMain::btnStart2_clicked()
{
    qDebug()<<"按钮2点击了";
    SendDirectionDlg* m_pDirectionDlg = new SendDirectionDlg;
    m_pDirectionDlg->setModal (true);
    m_pDirectionDlg->show ();
}

void FrMain::btnStart3_clicked()
{
    qDebug()<<"按钮3点击了";

    sv3.clear ();
    freq3.clear ();
    double distance = ui->leDistanse3->text ().toDouble ();
    double ML = ui->leSensitive3->text ().toDouble ();

    double startFreq = ui->leStartFreq3->text ().toDouble ();
    double freqSpace = ui->leFreqSpace3->text ().toDouble ();
    double stopFreq = ui->leStopFreq3->text ().toDouble();

    int number =1 + (stopFreq-startFreq)/freqSpace;    //计算点数

    //double V = ui->leMaxExtract->text ().toDouble ();
    //double VLoad = ui->leVolExtract->text ().toDouble ();

    qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //设置随机数种子

    for(int i=0; i<number; i++)
    {
        //生成7.0到9.0之间的随机浮点数
        double V1Max = 0.9;
        double V1Min = 0.7;

        double V2Max = 1.0;
        double V2Min = 0.8;

        double diffV1 = fabs(V1Max-V1Min);
        double diffV2 = fabs(V2Max-V2Min);

        double m1=(double)(qrand()%1000)/1000;//生成0.000到1.000的随机浮点数
        double retvalV1=V1Min+m1*diffV1;
        double retvalV2=V2Min+m1*diffV2;

        //v1.push_back (retvalV);
        //vLoad1.push_back (retvalVLoad);
        //qDebug()<<retvalV;
        //qDebug()<<retvalVLoad;

        double tempM1 = 20*log10(retvalV1/2.828);
        double tempM2 = 20*log10(retvalV2/2.828);
        double M = ML+tempM1-tempM2;

        sv3.push_back (M);         //纵坐标添加数据

        //qDebug()<<Sv;

    }

    for(int j=0 ; j<number ; j++)
    {
        freq3.push_back (startFreq+j*freqSpace);
    }


    RecvSensitiveDlg* m_pRecvSensitiveDlg = new RecvSensitiveDlg(freq3,sv3);
    m_pRecvSensitiveDlg->setModal (true);
    m_pRecvSensitiveDlg->show ();
}

void FrMain::btnStart4_clicked()
{
    qDebug()<<"按钮4点击了";

    RecvDirectionDlg* m_pRecvDirectionDlg = new RecvDirectionDlg;
    m_pRecvDirectionDlg->setModal (true);
    m_pRecvDirectionDlg->show ();
}

